package com.black.knight.chess.adapters;

import java.util.Arrays;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

import com.black.knight.chess.R;
import com.black.knight.chess.domain.PNGOpening.PNGOpenings;
import com.black.knight.chess.domain.PNGProblem.PNGProblems;

public class PGNGameAdapter extends CursorAdapter {
    
    private final LayoutInflater mInflater;

    public PGNGameAdapter(Context context, Cursor c) {
		super(context, c);
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {        
		TextView textViewId = (TextView)view.findViewById(R.id.rowId);
		TextView textView = (TextView)view.findViewById(R.id.rowDesc);

		String descString = cursor.getString(cursor.getColumnIndex(PNGOpenings.DESCRIPTION));
		
		if(Arrays.asList(cursor.getColumnNames()).contains(PNGProblems.MATE_IN)) {
			String mateString = "\n" + context.getResources().getString(R.string.mate_in) 
					+ " " + cursor.getString(cursor.getColumnIndex(PNGProblems.MATE_IN))
					+ " " + context.getResources().getString(R.string.mate_in_moves);
			
			descString += mateString;
		}
   		
   		textViewId.setText((cursor.getPosition() + 1) + ". ");
   		
    	
    	textView.setText(descString);
	}

	@Override
	public View newView(final Context context, Cursor cursor, ViewGroup parent) {
		final View view = mInflater.inflate(R.layout.game_row, parent, false);
		bindView(view, context, cursor);
		
		return view;
	}

    public long getItemId(int position) {
        return 0;
    }
}